package com.stylefeng.guns.modular.revenue.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.modular.revenue.dao.RevenueMapper;
import com.stylefeng.guns.modular.revenue.model.Revenue;
import com.stylefeng.guns.modular.revenue.service.IRevenueService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  盈亏服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-06-30
 */
@Service
public class RevenueServiceImpl extends ServiceImpl<RevenueMapper,Revenue> implements IRevenueService {
    private static Logger logger = LoggerFactory.getLogger(RevenueServiceImpl.class);

    @Override
    public List<Map<String,Object>> selectRevenue(String platform,String beginDate,String endDate){

        return this.baseMapper.selectRevenue(platform,beginDate,endDate);
    }

    /**
     * 添加报表
     * @param revenue
     * @throws RuntimeException
     */
    @Override
    public void addRevenue(Revenue revenue)throws GunsException{
        if (StringUtils.isBlank(revenue.getRevtime().toString())){
            logger.error("报表时间不能为空");
            throw new RuntimeException("报表时间不能为空");
        }
       baseMapper.addRevenue(revenue);

    }

    /**
     * 批量添加
     * @param revenues
     * @throws RuntimeException
     */
    @Override
    public void addRevenues(List<Revenue> revenues)throws GunsException{

        for (Revenue revenue : revenues) {
            if (StringUtils.isBlank(revenue.getRevtime().toString())) {
                logger.error("报表时间不能为空");
                throw new RuntimeException("报表时间不能为空");
            }
        }
        baseMapper.addRevenues(revenues);

    }

    @Override
    public void updateRevenue(Revenue revenue)throws GunsException {
        baseMapper.updateRevenue(revenue);
    }

    /**
     * 查询平台当前是否已经存记录
     * @param platform
     * @param revtime
     * @return
     * @throws RuntimeException
     */
    @Override
    public Revenue getViewPfr(String platform, Date revtime)throws RuntimeException{
        return  baseMapper.getViewPfr(platform,revtime);
    }

    /**
     * 统计当月的盈亏
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public Revenue getRevenueByMoth(String platform,Date beginDate,Date endDate) {
        return baseMapper.getRevenuesByDate(platform,beginDate,endDate);

    }



    /**
     * 导出获取数据
     */
    @Override
    public List<Map<String,Object>> getExcelList(String platform,String beginDate,String endDate) {
        //组装查询
        EntityWrapper<Revenue> wrapper = new EntityWrapper<Revenue>();
        String sqlSelect="AG_profit AS agProfit,\n" +
                " id,\n" +
                " platform,\n" +
                " BBin_profit AS bbinProfit,\n" +
                " PT_profit AS ptProfit,\n" +
                " MG_profit AS mgProfit,\n" +
                " DS_profit AS dsProfit,\n" +
                " VR_profit AS vrProfit,\n" +
                " KG_profit AS kgProfit,\n" +
                " SB_profit AS sbProfit,\n" +
                " GP_profit AS gpProfit,\n" +
                " lotter_profit AS lotterProfit,\n" +
                " rebate_profit AS rebateProfit,\n" +
                " hdzs_profit AS hdzsProfit,\n" +
                " creattime,\n" +
                " updatetime,\n" +
                " revtime ,\n" +
                "(AG_profit+BBin_profit+PT_profit+MG_profit+DS_profit+VR_profit+KG_profit+SB_profit+GP_profit+lotter_profit-rebate_profit-hdzs_profit) as sumProfit ";
        wrapper.setSqlSelect(sqlSelect);
        wrapper.eq("platform",platform);
        wrapper.ge("revtime", beginDate);
        wrapper.le("revtime", endDate);
        return baseMapper.selectMaps(wrapper);

    }

    /**
     * 导出获取数据 合计
     */
    @Override
    public List<Map<String,Object>> getExcelListTotal(String platform,String beginDate,String endDate) {
        //组装查询
        EntityWrapper<Revenue> wrapper = new EntityWrapper<Revenue>();
        String sqlSelect=" sum(AG_profit) AS agProfit,\n" +
                " sum(BBin_profit) AS bbinProfit,\n" +
                " sum(PT_profit) AS ptProfit,\n" +
                " sum(MG_profit) AS mgProfit,\n" +
                " sum(DS_profit) AS dsProfit,\n" +
                " sum(VR_profit) AS vrProfit,\n" +
                " sum(KG_profit) AS kgProfit,\n" +
                " sum(SB_profit) AS sbProfit,\n" +
                " sum(GP_profit) AS gpProfit,\n" +
                " sum(lotter_profit) AS lotterProfit,\n" +
                " sum(rebate_profit) AS rebateProfit,\n" +
                " sum(hdzs_profit) AS hdzsProfit,\n" +
                " sum((AG_profit+BBin_profit+PT_profit+MG_profit+DS_profit+VR_profit+KG_profit+SB_profit+GP_profit+lotter_profit-rebate_profit-hdzs_profit)) as sumProfit ";
        wrapper.setSqlSelect(sqlSelect);
        wrapper.eq("platform",platform);
        wrapper.ge("revtime", beginDate);
        wrapper.le("revtime", endDate);
        return baseMapper.selectMaps(wrapper);

    }


}
